package csloader.requests
{
	import csloader.util.KeyValuePair;
	import csloader.util.RequestMethod;
	
	import flash.net.URLRequest;
	import mx.managers.BrowserManager;
	import mx.managers.IBrowserManager;
	import mx.utils.Base64Encoder;
	import mx.utils.URLUtil;

	public class ProxyRequest extends RequestBase
	{
		public static var Server_Url:String = "";	//将此变量设空，则总是禁用跨域代理	proxy.aspx
		
		public function ProxyRequest(callback:Function)
		{
			super(callback);
		}
		
		override public function getRequest(url:String, parms:KeyValuePair=null, requestMethod:String=RequestMethod.Get, ...args) : URLRequest
		{
			if(ProxyRequest.Server_Url)
			{
				//得到当前主机名
				var browser:IBrowserManager = BrowserManager.getInstance();
				browser.init();
				var host:String = URLUtil.getServerNameWithPort(browser.base);
				//得到目标Url地址
				var fullUrl:String = URLUtil.getFullURL(host, url);			
				//跨域请求，代理调用
				if(host != URLUtil.getServerNameWithPort(fullUrl))
				{
					var base64:Base64Encoder = new Base64Encoder();
					base64.encode(url);			
					return super.getRequest(Server_Url + "?" + base64.toString(), parms, requestMethod, args);					
				}
			}
			//未启用服务端或策略文件有效，直接调用
			return super.getRequest(url, parms, requestMethod, args);
		}
		
	}
}